import 'package:core/core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class BaseRoot extends StatefulWidget {
  final Widget? body;
  final String? title;
  final List<Widget>? actions;
  final double? elevation;
  final bool centerTitle;
  final bool canBack;
  final VoidCallback? clickBack;
  final Color? bgColor;
  final Color? barColor;
  final bool resizeToAvoidBottomInset;
  final bool belowStatusBar;
  final PreferredSizeWidget? bottom;
  final bool? wantKeepAlive;
  final bool isDarkTheme;
  final PopInvokedCallback? onPopInvoked;
  final bool primary;
  final Widget? bottomNavigationBar;

  const BaseRoot({
    super.key,
    this.body,
    this.title,
    this.actions,
    this.elevation,
    this.centerTitle = true,
    this.canBack = true,
    this.clickBack,
    this.bgColor,
    this.barColor,
    this.resizeToAvoidBottomInset = false,
    this.belowStatusBar = true,
    this.bottom,
    this.wantKeepAlive,
    this.isDarkTheme = false,
    this.onPopInvoked,
    this.primary = true,
    this.bottomNavigationBar,
  });

  @override
  State<BaseRoot> createState() => _BaseRootState();
}

class _BaseRootState extends State<BaseRoot> {
  @override
  Widget build(BuildContext context) {
    Widget child = Scaffold(
      appBar: null == widget.title
          ? (widget.belowStatusBar
              ? PreferredSize(
                  preferredSize: Size.fromHeight(DeviceHelper.topBarHeight * 0.07),
                  child: const SafeArea(
                    top: true,
                    child: SizedBox(),
                  ),
                )
              : null)
          : AppBar(
              title: Text(widget.title!, style: Colors.black87.toStyle(36.px).withBold),
              elevation: widget.elevation ?? 1.px,
              shadowColor: Colors.grey,
              surfaceTintColor: Colors.white,
              scrolledUnderElevation: widget.elevation ?? 1.px,
              titleSpacing: NavigationToolbar.kMiddleSpacing,
              actions: widget.actions,
              backgroundColor: widget.barColor ?? Colors.white,
              leading: widget.canBack == true && Navigator.canPop(context)
                  ? IconButton(
                      icon: Icon(
                        Icons.arrow_back_ios,
                        color: Colors.black87,
                        size: 40.px,
                      ),
                      onPressed: widget.clickBack ??
                          () {
                            Navigator.of(context).pop();
                          },
                    )
                  : const SizedBox(),
              centerTitle: widget.centerTitle,
              bottom: widget.bottom,
              systemOverlayStyle: widget.isDarkTheme ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
            ),
      resizeToAvoidBottomInset: widget.resizeToAvoidBottomInset,
      body: SafeArea(child: widget.body ?? const Placeholder()),
      backgroundColor: widget.bgColor ?? Colors.white,
      primary: widget.primary,
      bottomNavigationBar: widget.bottomNavigationBar,
    );
    if (null != widget.onPopInvoked) {
      child = PopScope(
        canPop: false,
        onPopInvoked: widget.onPopInvoked,
        child: child,
      );
    }
    return child;
  }
}
